//
//  CYYNetWorkManager+GetMyAllOutDateParties.m
//  LetsParty
//
//  Created by rimi on 15/9/17.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYNetWorkManager+GetMyAllOutDateParties.h"

@implementation CYYNetWorkManager (GetMyAllOutDateParties)
/**< 获得所有历史活动记录（包括发起、参加、关注） */
+ (void)getMyAllOutDateParties:(SucessGet)sucessGet
                      fail:(Fail)fail{
    if ([CYYNetWorkManager currentUserGet] != NULL) {
        NSString * objectId = [CYYNetWorkManager currentUserGet].objectId;
        BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
        //构造约束条件
        BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
        [inQuery whereKey:@"objectId" equalTo:objectId];
        
        /**< 约束条件1 */
        [bquery whereKey:@"partyAllPeople" matchesQuery:inQuery];
        /**< 约束条件2 */
        
        //获得当前系统的时间
        NSDate * currentDate = [NSDate date];
        //时间转换成NSString
        NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
        [dateFormate setDateFormat:@"yyyy/MM/dd"];
        NSString * currentDateString = [dateFormate stringFromDate:currentDate];
        
        [bquery whereKey:@"partyEndTime" lessThan:currentDateString];
        
        //降序
        [bquery orderByDescending:@"partyBeginTime"];
        
        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (error) {
                fail(error);
            }else{
                sucessGet(array);
            }
        }];
    }else{
//        NSLog(@"当前用户没有登陆");
    }
}

/**< 获得改用用户所有历史关注的活动 */
+ (void)getMyAllOutDateObserveParties:(SucessGet)sucessGet
                       fail:(Fail)fail{
    NSString * objectId = [CYYNetWorkManager currentUserGet].objectId;
    BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
    //构造约束条件1
    BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
    [inQuery whereKey:@"objectId" equalTo:objectId];
    //匹配查询1
    [bquery whereKey:@"partyObservers" matchesQuery:inQuery];
    /**< 约束条件2 */
    
    //获得当前系统的时间
    NSDate * currentDate = [NSDate date];
    //时间转换成NSString
    NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
    [dateFormate setDateFormat:@"yyyy/MM/dd"];
    NSString * currentDateString = [dateFormate stringFromDate:currentDate];
    [bquery whereKey:@"partyEndTime" lessThan:currentDateString];
    
    //降序
    [bquery orderByDescending:@"partyBeginTime"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        }else{
            sucessGet(array);
        }
    }];
}

/**< 获得该用户所有历史参加的活动 */
+ (void)getMyAllOutDateTakePartyInParties:(SucessGet)sucessGet
                           fail:(Fail)fail{
    NSString * objectId = [CYYNetWorkManager currentUserGet].objectId;
    BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
    //构造约束条件
    BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
    [inQuery whereKey:@"objectId" equalTo:objectId];
    //匹配查询
    [bquery whereKey:@"partyParticipators" matchesQuery:inQuery];
    
    /**< 约束条件2 */
    
    //获得当前系统的时间
    NSDate * currentDate = [NSDate date];
    //时间转换成NSString
    NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
    [dateFormate setDateFormat:@"yyyy/MM/dd"];
    NSString * currentDateString = [dateFormate stringFromDate:currentDate];
    [bquery whereKey:@"partyEndTime" lessThan:currentDateString];
    
    //降序
    [bquery orderByDescending:@"partyBeginTime"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        }else{
            NSMutableArray * dataArray = [array mutableCopy];
            //移除参加者
            [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([[obj objectForKey:@"partySponsorInfo"][@"partySponsorObjectId"] isEqualToString:[CYYNetWorkManager currentUserGet].objectId]) {
                    [dataArray removeObject:obj];
                }
            }];
            sucessGet(array);
        }
    }];
}

/**< 获得该用户所有发起的活动历史记录 */
+ (void)getMyAllOutDateSponsorPartiesSucessGet:(SucessGet)sucessGet
                                fail:(Fail)fail{
    NSString * mobilePhoneNumber = [CYYNetWorkManager currentUserGet].mobilePhoneNumber;
    BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
    //构造约束条件
    BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
    [inQuery whereKey:@"mobilePhoneNumber" equalTo:mobilePhoneNumber];
    
    //匹配查询
    [bquery whereKey:@"partySponsor" matchesQuery:inQuery];
    //匹配查询2
    [bquery whereKey:@"partyAllPeople" matchesQuery:inQuery];
    
    /**< 约束条件2 */
    
    //获得当前系统的时间
    NSDate * currentDate = [NSDate date];
    //时间转换成NSString
    NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
    [dateFormate setDateFormat:@"yyyy/MM/dd"];
    NSString * currentDateString = [dateFormate stringFromDate:currentDate];
    [bquery whereKey:@"partyEndTime" lessThan:currentDateString];
    
    //降序
    [bquery orderByDescending:@"partyBeginTime"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        }else{
            sucessGet(array);
        }
    }];
}
@end
